package com.shuoyue.ycgk.ui.mine.mynote;

import com.shuoyue.ycgk.base.BaseModel;
import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.entity.ChapterType;
import com.shuoyue.ycgk.entity.PersonNote;
import com.shuoyue.ycgk.entity.QuestionNote;
import com.shuoyue.ycgk.entity.QuestionParent;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;

import java.util.List;

import io.reactivex.Observable;

public interface MyNoteContract {
    interface View extends BaseView {
        void setTypeList(List<ChapterType> chapterSectionTypes);

        void setQuestions(List<PersonNote> noteList);

        void praiseResult(PersonNote note, boolean isPraised);

        void deleteSuc(PersonNote questionNote);
    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();

        public void getType(int moduleId, int like) {
            apply(model.getUserNoteType(moduleId, like))
                    .subscribe(new ApiSubscriber<Optional<List<ChapterType>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<ChapterType>> listOptional) {
                            super.onNext(listOptional);
                            mView.setTypeList(listOptional.getIncludeNull());
                        }
                    });
        }

        public void getQuestions(Integer sectionId, int like, int n) {
            apply(model.getUserNoteQUestion(sectionId, like, n))
                    .subscribe(new ApiSubscriber<Optional<ListWithPage<PersonNote>>>(mView, this) {
                        @Override
                        public void onNext(Optional<ListWithPage<PersonNote>> listOptional) {
                            super.onNext(listOptional);
                        }
                    });
        }

        public void getQuestions(Integer sectionId, int like) {
            apply(model.getUserNoteQUestion(sectionId, like))
                    .subscribe(new ApiSubscriber<Optional<List<PersonNote>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<PersonNote>> listOptional) {
                            super.onNext(listOptional);
                            mView.setQuestions(listOptional.getIncludeNull());
                        }
                    });
        }

        public void praiseNote(PersonNote questionNote, int isAdd) {
            apply(model.praiseNote(questionNote.getId(), isAdd))
                    .subscribe(new ApiSubscriber<Optional<String>>(mView, this) {
                        @Override
                        public void onNext(Optional<String> dataOp) {
                            super.onNext(dataOp);
                            mView.praiseResult(questionNote, isAdd == 1);
                        }
                    });
        }

        public void deleteNote(PersonNote questionNote) {
            apply(model.deleteNote(questionNote.getId()))
                    .subscribe(new ApiSubscriber<Optional<String>>(mView, this) {
                        @Override
                        public void onNext(Optional<String> dataOp) {
                            super.onNext(dataOp);
                            mView.deleteSuc(questionNote);
                        }
                    });
        }
    }

    class Model extends BaseModel {
        Observable<BaseResult<List<ChapterType>>> getUserNoteType(int moduleId, int like) {
            return RetrofitClient.getInstance().getApi().getUserNoteType(moduleId, like);
        }

        Observable<BaseResult<ListWithPage<PersonNote>>> getUserNoteQUestion(Integer sectionId, int like, int n) {
            return RetrofitClient.getInstance().getApi().getUserNoteQUestion(sectionId, like, n);
        }

        Observable<BaseResult<List<PersonNote>>> getUserNoteQUestion(Integer sectionId, int like) {
            return RetrofitClient.getInstance().getApi().getUserNoteQUestion(sectionId, like);
        }

        Observable<BaseResult<String>> praiseNote(int actionId, int isAdd) {
            return RetrofitClient.getInstance().getApi().praiseNote(actionId, isAdd);
        }

        Observable<BaseResult<String>> deleteNote(int actionId) {
            return RetrofitClient.getInstance().getApi().deleteNote(actionId);
        }
    }
}
